package com.tigercel.xiangchengyun.ui.activities

import android.content.Intent
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.helper.ItemTouchHelper
import android.view.View
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.ChannelAdapter
import com.tigercel.xiangchengyun.common.MsgID
import com.tigercel.xiangchengyun.entity.NewsChannelTable
import com.tigercel.xiangchengyun.ui.BaseActivity
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.TitleBuilder
import com.tigercel.xiangchengyun.widget.ItemDragHelperCallback
import kotlinx.android.synthetic.main.activity_news_channel.*

class NewsChannelActivity : BaseActivity() {
    override val mContentLayoutResouredId: Int
        get() = R.layout.activity_news_channel
    private var channelAdapterMine: ChannelAdapter? = null
    private var newsChannels: MutableList<NewsChannelTable>? = null


    override fun initViews() {
        TitleBuilder(this)
                .setTitleText("更多")
                .setLeftImage(R.mipmap.ic_up_white)
                .setLeftOnClickListener(View.OnClickListener { finish() })
        ll_all_channel.setOnClickListener {
            startActivity(Intent(this, NewChannelMoreActivity::class.java))
        }

        channelAdapterMine = ChannelAdapter(mContext, R.layout.item_news_channel)

        news_channel_mine_rv.layoutManager = GridLayoutManager(this, 4, LinearLayoutManager.VERTICAL, false)
        news_channel_mine_rv.itemAnimator = DefaultItemAnimator()
        news_channel_mine_rv.adapter = channelAdapterMine

        newsChannels = ArrayList()
        newsChannels!!.add(NewsChannelTable("推荐","tj","",true,0,true))
        newsChannels!!.add(NewsChannelTable("项城","tj","",true,1,false))
        newsChannels!!.add(NewsChannelTable("政情","tj","",true,2,false))
        newsChannels!!.add(NewsChannelTable("福利","tj","",true,3,false))
        newsChannels!!.add(NewsChannelTable("天下","tj","",true,4,false))
        newsChannels!!.add(NewsChannelTable("生活","tj","",true,5,false))
        newsChannels!!.add(NewsChannelTable("直播","tj","",true,6,false))
        newsChannels!!.add(NewsChannelTable("专题","tj","",true,7,false))
        newsChannels!!.add(NewsChannelTable("VR全景","tj","",true,8,false))

        channelAdapterMine!!.replaceAll(newsChannels)
        channelAdapterMine!!.setOnItemClickListener { _, position ->
            val newsChannel = channelAdapterMine!!.get(position)
            channelAdapterMine!!.removeAt(position)
            // 发送频道列表改变广播
            Global.sendMsg(MsgID.NEWS_CHANNEL_CHANGED, channelAdapterMine!!.all as java.util.ArrayList<NewsChannelTable>)
        }


        val itemDragHelperCallback = ItemDragHelperCallback(channelAdapterMine)
        val itemTouchHelper = ItemTouchHelper(itemDragHelperCallback)
        itemTouchHelper.attachToRecyclerView(news_channel_mine_rv)
        channelAdapterMine!!.setItemDragHelperCallback(itemDragHelperCallback)
    }



}
